package ua.epam.muzalevskiy.traction.commands;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ua.epam.muzalevskiy.traction.configuretion.PagePathConfigurer;
import ua.epam.muzalevskiy.traction.dao.DaoFactory;
import ua.epam.muzalevskiy.traction.dao.RouteDao;
import ua.epam.muzalevskiy.traction.route.Route;

/**
 * <code>ViewSchedulePageCommand</code> class is the <code>Command</code> 
 * interface implementation, that realized <code>Command</code> pattern.
 * Current class encapsulate displaying page in user can select a number
 * of route, which schedule he wont`s to see.
 *
 * @author Muzalevskiy Vitaliy
 * @see Command
 * @see Route
 * @see RouteDao
 */
public class ViewSchedulePageCommand implements Command {

    private static DaoFactory factory = new DaoFactory();
    private RouteDao routeDao = factory.getRouteDao();
    private static final String ATTRIBUTE_NAME_ROUTES = "routes";
    
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        List<Route> routeList = routeDao.selectAll();
        String[] routes = new String[routeList.size()];
        for(int i = 0; i < routeList.size(); i++){
            routes[i] =routeList.get(i).getNumber();
        }
        request.setAttribute(ATTRIBUTE_NAME_ROUTES,routes);
        String page = PagePathConfigurer.getInstance()
                        .getProperty(PagePathConfigurer.SCHEDULE_PAGE);
        return page;
    }
    
}
